<?php

//string $Sport="Sport id";

$XML = new DOMDocument();
$XML->load("Sport.xml");
$xpath = new DOMXPath($XML);



/*
$XMLfra = new DOMDocument();
$XMLfra->load("leaguetable_Soccer.France.Ligue1.Ligue11112.xml");
$XMLeng = new DOMDocument();
$XMLeng->load("leaguetable_Soccer.England.PremierLeague.PremierLeague1112.xml");
$XMLita = new DOMDocument();
$XMLita->load("leaguetable_Soccer.Italy.SerieA.SerieA1112.xml");
$XMLger = new DOMDocument();
$XMLger->load("leaguetable_Soccer.Germany.Bundesliga.Bundesliga1112.xml");
*/
/*
$xpath = new DOMXPath($XML);
$xNog = $xpath->query("//ns2:SportradarData/Sport[@id='1']");
$xpath = new DOMXPath($XML);
$xKos = $xpath->query("//ns2:SportradarData/Sport[@id='2']");
*/
$d = dir("C:\bit\web\ponuda\public_html\test");
while (false !== ($entry = $d->read()))
{
  if(strpos($entry, "leaguetable")!==FALSE)
  {echo "*$entry.\n";
  $XMLfra = new DOMDocument();
  $XMLfra->load($entry);
  $xpathFra = new DOMXPath($XMLfra);
  $xCatSP = $xpathFra->query("//ns2:SportradarData/Sport/@id");
  $xCat = $xpathFra->query("//ns2:SportradarData/Sport/Category");
  $x = $xpath->query("//ns2:SportradarData/Sport[@id='" . $xCatSP->item(0)->textContent . "']");

  $xNew = $XML->importNode($xCat->item(0), true);
  $x->item(0)->appendChild($xNew);
  $XML->save("Sport_generated.xml");
  }
}
$d->close();
die("kraj");

$xpathFra = new DOMXPath($XMLfra);
$xCatSP = $xpathFra->query("//ns2:SportradarData/Sport/@id");
$xCat = $xpathFra->query("//ns2:SportradarData/Sport/Category");

$xpath = new DOMXPath($XML);
$x = $xpath->query("//ns2:SportradarData/Sport[@id='" . $xCatSP->item(0)->textContent . "']");

$xNew = $XML->importNode($xCat->item(0), true);
$x->item(0)->appendChild($xNew);
$XML->save("Sport_generated.xml");
die("kraj");

//echo $xCatFra->item(0)->textContent;





$xpathEng = new DOMXPath($XMLeng);
$xCatEng = $xpathEng->query("//ns2:SportradarData/Sport/Category");
$xpathIta = new DOMXPath($XMLita);
$xCatIta = $xpathIta->query("//ns2:SportradarData/Sport/Category");
$xpathGer = new DOMXPath($XMLger);
$xCatGer = $xpathGer->query("//ns2:SportradarData/Sport/Category");

$xCat = $XML->importNode($xCatFra->item(0), true);
$xNog->item(0)->appendChild($xCat);
$xCat = $XML->importNode($xCatEng->item(0), true);
$xNog->item(0)->appendChild($xCat);
$xCat = $XML->importNode($xCatIta->item(0), true);
$xNog->item(0)->appendChild($xCat);
$xCat = $XML->importNode($xCatGer->item(0), true);
$xNog->item(0)->appendChild($xCat);


$XMLBaus = new DOMDocument();
$XMLBaus->load("leaguetable_Basketball.Austria.ABL.ABL1112.xml");
$XMLBcro = new DOMDocument();
$XMLBcro->load("leaguetable_Basketball.Croatia.A1LeagueMen.A1Liga1112MainRound.xml");
$XMLBfra = new DOMDocument();
$XMLBfra->load("leaguetable_Basketball.France.ProA.ProA1112.xml");
$XMLBger = new DOMDocument();
$XMLBger->load("leaguetable_Basketball.Germany.BBL.BBL1112.xml");

$xpathFra = new DOMXPath($XMLBaus);
$xCatFra = $xpathFra->query("//ns2:SportradarData/Sport/Category");
$xpathEng = new DOMXPath($XMLBcro);
$xCatEng = $xpathEng->query("//ns2:SportradarData/Sport/Category");
$xpathIta = new DOMXPath($XMLBfra);
$xCatIta = $xpathIta->query("//ns2:SportradarData/Sport/Category");
$xpathGer = new DOMXPath($XMLBger);
$xCatGer = $xpathGer->query("//ns2:SportradarData/Sport/Category");

$xCat = $XML->importNode($xCatFra->item(0), true);
$xKos->item(0)->appendChild($xCat);
$xCat = $XML->importNode($xCatEng->item(0), true);
$xKos->item(0)->appendChild($xCat);
$xCat = $XML->importNode($xCatIta->item(0), true);
$xKos->item(0)->appendChild($xCat);
$xCat = $XML->importNode($xCatGer->item(0), true);
$xKos->item(0)->appendChild($xCat);

// $xpathBaus = new DOMXPath($XMLBaus);
// $xCatBaus = $xpathBaus->query("//ns2:SportradarData/Sport/Category");
// $xpathBcro = new DOMXPath($XMLBcro);
// $xCatBcro = $xpathBcro->query("//ns2:SportradarData/Sport/Category");
// $xpathBfra = new DOMXPath($XMLBfra);
// $xCatBfra = $xpathBfra->query("//ns2:SportradarData/Sport/Category");
// $xpathBger = new DOMXPath($XMLBger);
// $xCatBger = $xpathBger->query("//ns2:SportradarData/Sport/Category");

// $xCat = $XML->importNode($xCatBaus->item(0), true);
// $xKos->item(0)->appendChild($xCat);
// $xCat = $XML->importNode($xCatBcro->item(0), true);
// $xKos->item(0)->appendChild($xCat);
// $xCat = $XML->importNode($xCatBfra->item(0), true);
// $xKos->item(0)->appendChild($xCat);
// $xCat = $XML->importNode($xCatBger->item(0), true);
// $xKos->item(0)->appendChild($xCat);


$XML->save("Sport_generated.xml");

